package china.accountinghelper.adapter;

import com.xdroid.base.AdapterBase;
import china.wc.tallyhelper.R;
import china.wc.tallyhelper.business.BusinessCategory;
import china.wc.tallyhelper.model.ModelCategory;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * 本类是类别管理页面中，CategoryListView用的Adapter。
 * 
 * @author 王超
 * 
 */
public class AdapterListViewCategoryList extends AdapterBase<ModelCategory>
{
  private BusinessCategory mBusinessCategory;

  public AdapterListViewCategoryList(Context pContext)
  {
    super(pContext, null);
    mBusinessCategory = new BusinessCategory();
    setList(mBusinessCategory.select());
  }
  
  @Override
  public long getItemId(int pPosition)
  {
      return getList().get(pPosition).getID();
  }

  // 返回绑定好CategoryName的View
  @Override
  public View getView(int pPosition, View pConvertView, ViewGroup pParent)
  {

    if (pConvertView == null)
    {
      pConvertView = this.getLayoutInflater().inflate(R.layout.item_listview_categorylist, null);
    }

    ModelCategory _ModelCategoryObject = getList().get(pPosition);

    TextView _TextView = (TextView) pConvertView.findViewById(R.id.ItemListViewCategoryList_TextView_Name);
    _TextView.setText(_ModelCategoryObject.getName());

    return pConvertView;
  }

  /**
   * 数据库中数据改变后，需要重新setList()，否则调用本方法不会刷新数据。
   */
  @Override
  public void notifyDataSetChanged()
  {
    setList(mBusinessCategory.select());
    super.notifyDataSetChanged();
    
  }

}
